<template>
  <div >
    <form-head @onSubmit="onSubmit" :is_change="is_change"/>
    <el-form ref="form" :rules="rules" :model="form" label-width="100px" class="layout-all form-width">
      <el-form-item label="管理员:" prop="username">
        <el-input v-model="form.username" placeholder="请输入管理员" />
      </el-form-item>
      <el-form-item label="原密码:" prop="password2" v-if="this.id==1">
        <el-input
          v-model="form.password_old"
          placeholder="请输入原密码(不修改不用填)"
        />
      </el-form-item>
      <el-form-item label="密码:" prop="password" v-if="this.id==0">
        <el-input
          v-model="form.password"
          placeholder="请输入密码"
        />
      </el-form-item>
      <el-form-item label="密码:" prop="password1" v-else>
        <el-input
          v-model="form.password"
          placeholder="请输入新密码(不修改不用填)"

        />
      </el-form-item>
      <el-form-item label="昵称:" prop="name">
        <el-input v-model="form.name" placeholder="请输入昵称" />
      </el-form-item>
       <el-form-item prop="headimgurl" label="头像地址:">
         <upload-pic :thumb="form.headimgurl" @update:thumb="form.headimgurl=$event" width="100" cate_id="5"/>
      </el-form-item>

      <el-form-item label="权限:" prop="role_id" >
        <!-- <el-input v-model="form.content" type="textarea" :rows="5" placeholder="请输入公告内容" /> -->
        <el-select v-model="form.role_id" placeholder="请选择权限">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用:" prop="status">
        <el-switch
          v-model="form.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name:'admin_form',
  data() {
    return {
      id: 0,
      is_change: undefined,
      options: [],
      form: {
        id: 0,
        username: "",
        password: "",
        name: "",
        role_id: '',
        status: true,
        role: null,
      },
      rules: {
        username: [
          { required: true, message: "请输入管理员", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        role_id: [{ required: true, message: "请选择权限组", trigger: "blur" }],
      },
    };
  },
  mounted() {
    this.id = (this.$route.params && this.$route.params.id)||0
    this.$setTagsViewTitle(this.id)
    this.init_into();
  },
  methods: {
    // 提交
    onSubmit(formName) {
      const form = this.form;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          form.id = this.id;
          this.get("/admin/admin/save", form, "POST").then((e) => {
            this.$msg_ok("success", "保存成功");
            this.$back();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
      console.log("submit!");
    },
    // 返回
    bk() {
      this.$router.back();
    },
    // 获取当前数据
    init_into() {
      this.get("/admin/admin/form?id=" + this.id, "POST").then((e) => {
        if(this.id > 0){
          this.form = e.info
          this.form.status=this.form.status==1
        }
        
        this.options = e.role;
        setTimeout(()=>{this.is_change=false},100)
      });
    },
  },
  watch: {
    form:{
      handler (val, oldVal) {
	      if(this.is_change!=undefined)
        	this.is_change=true
      },
      deep:true
    }
  }
};
</script>

<style>
.layout-all {
  margin: 10px;
}
</style>
